/* -*- Mode: C -*- */
/******************************************************************************
*
*  Source File Name = insertx.sqx
*
*  Component Name   = DB2 XML Extender
*
*  Descriptive Name = Test dxxInsertXML
*
*  Copyright = 5622-044 (c) Copyright IBM Corp 1987, 1997, 2001,2002
*              Licensed Materials - Program Property of IBM
*
*  Status = New code
*
*  Function = This client program tests the dxxInsertXML stored
*             procedure of DB2 XML Extender.
*             It takes the arguments from the command line and calls the stored
*             procedure dxxInsertXML.
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlutil.h>
#include <sqlenv.h>
#include "sqlca.h"
#include "dxx.h"
#include "dxxrc.h"

/*******************************************************************
 * main
 *******************************************************************/
int main(int argc, char *argv[]) {
EXEC SQL INCLUDE SQLCA;
  int length = 0;
EXEC SQL BEGIN DECLARE SECTION;
  char    dbname[18];
  char    collection_name[129]; 
  SQL TYPE is CLOB_FILE     xmlfile;  /* input XML document */
  SQL TYPE is CLOB(1M)      *xmlobjp=NULL;  /* input XML document in memory */
  SQL TYPE is CLOB(100M)      *largexmlobjp = NULL;
  sqlint32 errCode;
  char    msgtext[2048];
  short   null_ind = -1;
  short   non_null_ind = 0;
  short   errCode_ind;
  short   msgtext_ind;
EXEC SQL END DECLARE SECTION;
  int     rc = 0, len;
  FILE   *testfile;
  int large=0; // indicates whether flag "-large" present
  char   errorMsg[1024] ; 
  /* Process command-line arguments */
  if (argc != 4 && argc != 5) {
    printf("Usage: %s [-large] dbname collection_name xmlfilename\n", argv[0]);
    return -1;
  }
  if (5 ==argc) {
    if (strcmp(argv[1], "-large")!=0) {
	  printf("Usage: %s [-large] dbname collection_name xmlfilename\n", argv[0]);
      return -1;
    } else {
	  large=1;
	  strcpy(dbname,         argv[2]);
	  strcpy(collection_name, argv[3]);
	  strcpy(xmlfile.name,    argv[4]);
	}
  } else {
	strcpy(dbname,         argv[1]);
	strcpy(collection_name, argv[2]);
	strcpy(xmlfile.name,    argv[3]);
	
  }
  /* Read the xml file to buffer */

  /* Connect to database */
  fprintf(stdout, "Connecting to database %s\n", dbname);
  EXEC SQL CONNECT TO :dbname;

  /* Initialize the XML CLOB object. */
  xmlfile.name_length = strlen(xmlfile.name);

  testfile = fopen( xmlfile.name, "r" );
  if ( testfile != NULL ) {
	 fclose( testfile );
  }
  else {
	fprintf(stderr, "fopen() error.\n");
	fprintf(stderr, "Error accessing file: %s \n", xmlfile.name);
	rc = -1;
	goto exit;
  }
  xmlobjp= (struct xmlobjp_t *)calloc(1, sizeof(struct xmlobjp_t));
  if (xmlobjp == NULL) {
	printf("out of memory\n");
	goto exit;
  }
  xmlfile.file_options = SQL_FILE_READ;
  /* DB2 would pass garbage if we directly fed
     xmlfile into our stored procedure. */
  if (large==0)	 {
    xmlobjp =  (struct xmlobjp_t *) malloc( sizeof (*xmlobjp));
    if (xmlobjp ==NULL) {
      fprintf(stderr, "Malloc() error.\n");
      goto exit;
    }
	EXEC SQL VALUES (:xmlfile) INTO :*xmlobjp;
	EXEC SQL CALL db2xml.dxxInsertXML (
                         :collection_name:non_null_ind,
                         :*xmlobjp:non_null_ind,
                         :errCode:errCode_ind,
                         :msgtext:msgtext_ind);
  } else {
    largexmlobjp =  (struct largexmlobjp_t *) malloc( sizeof (*largexmlobjp));
    if (largexmlobjp ==NULL) {
      fprintf(stderr, "Malloc() error.\n");
      goto exit;
    } 
    EXEC SQL VALUES (:xmlfile) INTO :*largexmlobjp;
	EXEC SQL CALL db2xml.dxxInsertXML100MB (
                         :collection_name:non_null_ind,
                         :*largexmlobjp:non_null_ind,
                         :errCode:errCode_ind,
                         :msgtext:msgtext_ind);
  }

  if ( SQLCODE >= 0) { 
    /* Print the output parms. */
    if( SQLCODE > 0 ){
       sqlaintp( errorMsg , 1024 , 80 , &sqlca );
	   printf("\n The warning message is \"%s\"  and it is being ignored.\n"
            , errorMsg );
    }          
    printf(" errCode=%d:%d\n  msgtext'%s':%d\n", 
             errCode, errCode_ind, msgtext, msgtext_ind);   

	if (errCode < DXX_ERRCODE_OK)
	  EXEC SQL ROLLBACK;
	else
	  EXEC SQL COMMIT;
  }
  else
  {
	sqlaintp( errorMsg , 1024 , 80 , &sqlca );
	printf("\n The error message is %s \n" , errorMsg );
	EXEC SQL ROLLBACK ;
  }
  EXEC SQL CONNECT RESET;

  rc = errCode;
exit:
  if (xmlobjp)
	free(xmlobjp);
  if (largexmlobjp)
    free(largexmlobjp);
  return rc;
}
